/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 *
 * 			http://www.kkkd.com/
 *
 * ========================================================
 */
package com.simon.mengkou.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.utils.UtilDevice;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Follow;
import com.simon.mengkou.data.bean.base.Image;
import com.simon.mengkou.data.bean.base.ImageTag;
import com.simon.mengkou.data.bean.base.Product;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.view.BasePagerAdapter;
import com.simon.mengkou.ui.view.label.AnimateLabelView;
import com.simon.mengkou.ui.view.label.LabelInfo;
import com.simon.mengkou.utils.UtilOuer;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/22.
 * desc    :
 */
public class ShareOrderImageAdapter extends BasePagerAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<Image> mList;
    private int mWidth;

    public ShareOrderImageAdapter(Context context, List<Image> datas) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mList = datas;
        mWidth = UtilDevice.getDevice(context).getWidth();
    }

    @Override
    public int getCount() {
        return UtilList.getCount(mList);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == (View) object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        final Image data = mList.get(position);
        View convertView = mInflater.inflate(R.layout.layout_share_order_image_item, null);
        SimpleDraweeView sdvImage = (SimpleDraweeView) convertView.findViewById(R.id.image_id_image);
        OuerApplication.mImageLoader.loadImage(sdvImage, data.getUrl());

        final RelativeLayout rlTagRoot = (RelativeLayout)convertView.findViewById(R.id.image_id_root);
        rlTagRoot.getLayoutParams().height = mWidth;
        rlTagRoot.removeAllViews();

        rlTagRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(int i=0; i<rlTagRoot.getChildCount(); i++) {
                    AnimateLabelView label = (AnimateLabelView) rlTagRoot.getChildAt(i);
                    if(label.getVisibility() == View.VISIBLE) {
                        label.setVisibility(View.GONE);
                    } else {
                        label.setVisibility(View.VISIBLE);
                    }
                }
            }
        });

        List<ImageTag> tags = data.getTags();
        if(UtilList.isNotEmpty(tags)) {
            for (ImageTag tag : tags) {
                LabelInfo info = new LabelInfo();
                info.title1Text = tag.getBrand();
                info.title2Text = tag.getPrice();
                info.title3Text = tag.getLocation();
                info.pcX = tag.getXratio();
                info.pcY = tag.getYratio();
                AnimateLabelView label = new AnimateLabelView(mContext);
                label.setLabelInfo(info);
                label.alwaysWave();

                rlTagRoot.addView(label);
            }
        }

        container.addView(convertView);
        return convertView;
    }
}
